package com.tongtech.proxy.core.protocol;

import java.util.ArrayList;
import java.util.List;

public class ProxyProtocolCheck {
    public static boolean check(byte[] data) {
        // 读取 RESP 响应
        String resp = new String(data, 0, data.length);

        // 检查 RESP 响应
        List<String> errors = new ArrayList<>();
        if (!resp.startsWith("*")) {
            errors.add("RESP response must start with '*'");
        } else {
            int count = Integer.parseInt(resp.substring(1));
            for (int i = 0; i < count; i++) {
                String line = resp.split("\r\n")[i + 1];
                if (!line.startsWith("$") && !line.startsWith("-") && !line.startsWith("+")) {
                    errors.add("Invalid RESP line: " + line);
                }
            }
        }

        // 打印错误信息
        if (errors.isEmpty()) {
            System.out.println("RESP response is valid");
            return true;
        } else {
            for (String error : errors) {
                System.err.println(error);
                return false;
            }
            return false;
        }



    }
}
